//============================================================================
// Name        : Falcon.cpp
// Author      : Eric (Li, Xiaoyi)
// Version     :
// Copyright   : Your copyright notice

//============================================================================
#include <vector>

#include "../include/includes.h"
#include "../DocumentProcessor/CDocumentProcessor.h"
#include "../DocumentParsers/CWikiMarkupParser.h"
#include "../DocumentRepresentation/CDocument.h"
#include "../RetrievalProcessor/RetrievalProcessor.h"
#include "../include/config.h"
#include  "../QueryProcessor/BatchQueryProcessor.h"
#include  "../QueryProcessor/RealTimeQueryProcessor.h"
#include "../ScoreCoculator/StaticScoreCalc.h"

using namespace std;

#define INDEX_MODE 'i';
#define BATCH_QUERY_MODE 'r';
#define REALTIME_QUERY_MODE 's';

int main(int argc, char** argv) {
	map<string, int> scoreModeMapping;
	scoreModeMapping.insert(make_pair("--bool", 0));
	scoreModeMapping.insert(make_pair("--vector", 1));
	scoreModeMapping.insert(make_pair("--okapi", 2));
	//////////////////////////////////////////////////////////////////////////////
	if (!CUtilities::checkCommand(argv)) {
		exit(0);
	}
	// cout << "Sample C++ codes for CSE 535" << endl; // prints Sample C++ codes for CSE 535

	config::path = argv[2];
	config* cf = config::getInstance();
	string base_dir = cf->getValue("path_to_input_data_dir");
	char mode = argv[1][1];

	CDocumentProcessor dp;
	RetrievalProcessor rp;
	StaticScoreCalc* scoreCalc;
	int scoreModelKey = scoreModeMapping[ cf->getValue("score_mode") ];
	string queryLine;

	string staticScoreFile;


	switch (mode) {
	//indexing mode
	case 'i':
		dp.count_files_to_process(base_dir);
		scoreCalc = StaticScoreCalc::getInstance();
		scoreCalc->openStream();
		cout << "num files to process = " << dp.get_num_files_to_process()
				<< endl;
		dp.process_directory(base_dir);
		scoreCalc->closeStream();
		break;
		//batch query mode
	case 'r':
		cout << "Batch Mode" << endl;
		BatchQueryProcessor::getInstance()->setFilePath(argv[3]);
		rp.setQueryMode(mode);
		rp.setBatchFilePath(argv[3]);
		rp.init();
		rp.processRetrieve(scoreModelKey);
		break;
		//real time query mode
	case 's':
		cout << "Interactive Mode with static score" << endl;
		rp.setQueryMode(mode);
		rp.staticScoreFile = argv[3];
		rp.init();
		do {
			cout << "dig>";
			getline(cin, queryLine);
			if (queryLine == "@exit") {
				cout << "Goodbye!" << endl;
				break;
			}
			RealTimeQueryProcessor::getInstance()->setQueryString(queryLine);
			rp.processRetrieve(scoreModelKey);
		} while (true);
		break;
		// From Web Browser
	case 'w':
		break;
	default:
		cout << "bad mode command" << endl;
		break;
	}

	return 0;
}
